/**
 * 
 */
package com.gr.staffpm.pages.admin.role;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.util.CollectionModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.Instant;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Permission;
import com.gr.staffpm.datatypes.Role;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.pages.behaviour.FocusOnLoadBehavior;
import com.gr.staffpm.pages.error.UnauthorizedPage;
import com.gr.staffpm.security.constants.StaffPMRoles;
import com.gr.staffpm.security.service.UserService;


/**
 * @author Graham Rhodes 2 May 2011 21:13:03
 */
@MountHybrid
@MountPath(path = "secure/admin/AddRole")
public class AddRolePage extends StaffPMWebPage {

    @SpringBean(name = "userService")
    private UserService userService;
    private final Role newRole = new Role();
    private Palette<Permission> permissionsPalette;
    private final TextField<String> role = new TextField<String>("role", new StringModel(""));
    private final TextField<String> level = new TextField<String>("level", new StringModel(""));

    public AddRolePage() {
        //TODO Check create role permission
        if (SecurityUtils.getSubject().hasRole(StaffPMRoles.ADMIN)) {

            Form<Role> form = new Form<Role>("addRoleForm") {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onSubmit() {
                    AddRolePage.this.onSubmit();
                }
            };
            add(form);

            add(new FeedbackPanel("feedback"));

            role.setRequired(true);
            role.add(new FocusOnLoadBehavior());
            form.add(role);

            level.setRequired(true);
            form.add(level);

            List<Permission> permissions = userService.getAllPermissions();
            IChoiceRenderer<Permission> permissionRenderer = new ChoiceRenderer<Permission>("permission", "id");
            permissionsPalette =
                    new Palette<Permission>(
                            "permissions",
                            new ListModel<Permission>(new ArrayList<Permission>()),
                            new CollectionModel<Permission>(permissions),
                            permissionRenderer,
                            10,
                            false);
            form.add(permissionsPalette);

            Button cancel = new Button("cancelButton") {

                private static final long serialVersionUID = 1L;

                @Override
                public void onSubmit() {
                    setResponsePage(AdminRolesPage.class);
                }

            };
            cancel.setDefaultFormProcessing(false);
            form.add(cancel);

        } else {
            setResponsePage(UnauthorizedPage.class);
        }
    }

    @SuppressWarnings("unchecked")
    protected void onSubmit() {
        User currUser = userService.getCurrentUser();

        newRole.setRole(role.getValue());
        newRole.setLevel(Integer.parseInt(level.getValue()));
        newRole.setLastUpdated(new Instant().toDate());
        newRole.setUpdatedBy(currUser);
        newRole.setCreatedBy(currUser);
        newRole.setCreated(new Instant().toDate());
        newRole.getPermissions().addAll((List<Permission>) permissionsPalette.getDefaultModelObject());

        userService.createOrUpdateRole(newRole);

        //Send user back to previous page
        setResponsePage(new AdminRolesPage());
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.StaffPMWebPage#getTitle()
     */
    @Override
    public String getTitle() {
        return "StaffPM - Add Role";
    }

}
